﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.MEP;
using YArchitech.Revit;
using HYFamilyLibDataManager;

namespace YArchitech.Plumbing
{
	public partial class DrainageSystemSettingForm : System.Windows.Forms.Form
	{
		public DrainageSystemSettingForm(Autodesk.Revit.DB.Document document)
		{
			this.InitializeComponent();
			this.doc = document;
			this.InitialTreeViewInfo();
			this.tvFitting.SelectedNode = this.tvFitting.Nodes[0].Nodes[0];
			new YJKProcessTools().ShowInTaskbarChange(this);
			TreeViewTools.SetTreeViewFocus(this.tvFitting);
			this.SetFormControl();
			this.segmentFitting = new OperateSegmentFitting(this.doc);
			this.dsSetting = HYDrainageSystemSetting.Instance;
			this.dsSetting.InitialPipeSystemType(this.doc);
			this.BandingCmbBox();
			this.GetFamilyList();
			this.InitialFormInfo(this.dsSetting.PipeTypeName);
		}

		private string GetFamilyName(string tvNodeText)
		{
			string result = "";
			uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(tvNodeText);
			if (num <= 1988809106u)
			{
				if (num <= 39831008u)
				{
					if (num != 39565431u)
					{
						if (num == 39831008u)
						{
							if (tvNodeText == "P弯")
							{
								result = this.dsSetting.PBendFamilyFile;
							}
						}
					}
					else if (tvNodeText == "S弯")
					{
						result = this.dsSetting.SBendFamilyFile;
					}
				}
				else if (num != 1574458390u)
				{
					if (num == 1988809106u)
					{
						if (tvNodeText == "通气帽")
						{
							result = this.dsSetting.VentilationCapFamilyFile;
						}
					}
				}
				else if (tvNodeText == "清扫口")
				{
					result = this.dsSetting.CleanMouthFamilyFile;
				}
			}
			else if (num <= 2445487072u)
			{
				if (num != 2367522647u)
				{
					if (num == 2445487072u)
					{
						if (tvNodeText == "地漏")
						{
							result = this.dsSetting.FDrainFamilyFile;
						}
					}
				}
				else if (tvNodeText == "检查口")
				{
					result = this.dsSetting.CheckMouthFile;
				}
			}
			else if (num != 2636587616u)
			{
				if (num != 3158110106u)
				{
					if (num == 4145267678u)
					{
						if (tvNodeText == "弯头")
						{
							result = this.dsSetting.ElbowFamilyFile;
						}
					}
				}
				else if (tvNodeText == "三通")
				{
					result = this.dsSetting.TeeFamilyFile;
				}
			}
			else if (tvNodeText == "四通")
			{
				result = this.dsSetting.CrossFamilyFile;
			}
			return result;
		}

		private void SetFormControl()
		{
			base.Icon = Resource.YArchitechIcon;
			this.btnMaterial.Visible = false;
			this.cboMethod.Visible = false;
			this.lblMethod.Visible = false;
			this.dgvPipeSpecification.AutoGenerateColumns = false;
		}

		private void GetFamilyList()
		{
			this.listElbow = this.segmentFitting.GetDBFamilyList(HYFamilySymbolSign.Elbow);
			this.listTee = this.segmentFitting.GetDBFamilyList(HYFamilySymbolSign.Tee);
			this.listCross = this.segmentFitting.GetDBFamilyList(HYFamilySymbolSign.Cross);
			this.listSBend = this.segmentFitting.GetDBFamilyList(HYFamilySymbolSign.SBend);
			this.listPBend = this.segmentFitting.GetDBFamilyList(HYFamilySymbolSign.PBend);
			this.listCleanMouth = this.segmentFitting.GetDBFamilyList(HYFamilySymbolSign.CleanMouth);
			this.listVentilationCap = this.segmentFitting.GetDBFamilyList(HYFamilySymbolSign.VentilationCap);
			this.listFDrain = this.segmentFitting.GetDBFamilyList(HYFamilySymbolSign.FloorDrain);
			this.listCheckMouth = this.segmentFitting.GetDBFamilyList(HYFamilySymbolSign.CheckMouth);
		}

		private List<DbFamily> GetSpecificFamilyByName(string typeName)
		{
			List<DbFamily> result = null;
			uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(typeName);
			if (num <= 2367522647u)
			{
				if (num <= 1574458390u)
				{
					if (num != 39565431u)
					{
						if (num != 39831008u)
						{
							if (num == 1574458390u)
							{
								if (typeName == "清扫口")
								{
									result = this.listCleanMouth;
								}
							}
						}
						else if (typeName == "P弯")
						{
							result = this.listPBend;
						}
					}
					else if (typeName == "S弯")
					{
						result = this.listSBend;
					}
				}
				else if (num != 1632927359u)
				{
					if (num != 1988809106u)
					{
						if (num == 2367522647u)
						{
							if (typeName == "检查口")
							{
								result = this.listCheckMouth;
							}
						}
					}
					else if (typeName == "通气帽")
					{
						result = this.listVentilationCap;
					}
				}
				else if (typeName == "附件")
				{
					result = this.listVentilationCap;
				}
			}
			else if (num <= 2636587616u)
			{
				if (num != 2377983342u)
				{
					if (num != 2445487072u)
					{
						if (num == 2636587616u)
						{
							if (typeName == "四通")
							{
								result = this.listCross;
							}
						}
					}
					else if (typeName == "地漏")
					{
						result = this.listFDrain;
					}
				}
				else if (typeName == "管件")
				{
					result = this.listElbow;
				}
			}
			else if (num != 3158110106u)
			{
				if (num != 3404813138u)
				{
					if (num == 4145267678u)
					{
						if (typeName == "弯头")
						{
							result = this.listElbow;
						}
					}
				}
				else if (typeName == "设备")
				{
					result = this.listFDrain;
				}
			}
			else if (typeName == "三通")
			{
				result = this.listTee;
			}
			return result;
		}

		private void BandingCmbBox()
		{
			foreach (HYMaterial hymaterial in this.dsSetting.ListMaterial)
			{
				this.cboMaterial.Items.Add(hymaterial.Name);
				this.cboAnnotate.Items.Add(hymaterial.Annotate);
			}
		}

		private void InitialFormInfo(string materialName)
		{
			foreach (HYMaterial hymaterial in this.dsSetting.ListMaterial)
			{
				if (!(hymaterial.Name != materialName))
				{
					this.cboMaterial.SelectedItem = hymaterial.Name;
					this.cboAnnotate.SelectedItem = hymaterial.Annotate;
					this.txtRoughness.Text = hymaterial.Roughness.ToString();
					List<HYPipeSpecification> list = new List<HYPipeSpecification>();
					foreach (HYPipeSpecification item in hymaterial.Specifications)
					{
						list.Add(item);
					}
					this.dgvPipeSpecification.DataSource = list;
				}
			}
		}

		private void cboMaterial_SelectedIndexChanged(object sender, EventArgs e)
		{
			string materialName = this.cboMaterial.SelectedItem.ToString();
			this.InitialFormInfo(materialName);
		}

		private void InitialTreeViewInfo()
		{
			TreeNode treeNode = TreeViewTools.CreatTreeViewNode("管件", "管件");
			TreeNode node = TreeViewTools.CreatTreeViewNode("弯头", "弯头");
			treeNode.Nodes.Add(node);
			node = TreeViewTools.CreatTreeViewNode("三通", "三通");
			treeNode.Nodes.Add(node);
			node = TreeViewTools.CreatTreeViewNode("四通", "四通");
			treeNode.Nodes.Add(node);
			node = TreeViewTools.CreatTreeViewNode("S弯", "S弯");
			treeNode.Nodes.Add(node);
			node = TreeViewTools.CreatTreeViewNode("P弯", "P弯");
			treeNode.Nodes.Add(node);
			this.tvFitting.Nodes.Add(treeNode);
			treeNode = TreeViewTools.CreatTreeViewNode("附件", "附件");
			node = TreeViewTools.CreatTreeViewNode("通气帽", "检查口");
			treeNode.Nodes.Add(node);
			node = TreeViewTools.CreatTreeViewNode("清扫口", "清扫口");
			treeNode.Nodes.Add(node);
			node = TreeViewTools.CreatTreeViewNode("检查口", "检查口");
			treeNode.Nodes.Add(node);
			this.tvFitting.Nodes.Add(treeNode);
			treeNode = TreeViewTools.CreatTreeViewNode("设备", "设备");
			node = TreeViewTools.CreatTreeViewNode("地漏", "地漏");
			treeNode.Nodes.Add(node);
			this.tvFitting.Nodes.Add(treeNode);
		}

		private void tvFitting_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (e.Node.Parent == null)
			{
				this.tvFitting.SelectedNode = e.Node.Nodes[0];
			}
			string text = this.tvFitting.SelectedNode.Text;
			List<DbFamily> specificFamilyByName = this.GetSpecificFamilyByName(text);
			string familyName = this.GetFamilyName(text);
			this.BandListView(specificFamilyByName);
			this.SetListViewFocus(familyName);
			this.lsvFamily.Focus();
		}

		private void BandListView(List<DbFamily> listDBFamily)
		{
			this.lsvFamily.Items.Clear();
			this.lsvFamily.LargeImageList.Images.Clear();
			foreach (DbFamily dbFamily in listDBFamily)
			{
				this.lsvFamily.LargeImageList.Images.Add(dbFamily.Name, dbFamily.Image);
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = dbFamily.Name;
				listViewItem.Tag = dbFamily;
				listViewItem.ImageKey = dbFamily.Name;
				this.lsvFamily.Items.Add(listViewItem);
			}
		}

		private void SetListViewFocus(string itemName)
		{
			foreach (object obj in this.lsvFamily.Items)
			{
				ListViewItem listViewItem = (ListViewItem)obj;
				if (listViewItem.Text == itemName)
				{
					listViewItem.Selected = true;
					this.lsvFamily.Select();
					this.lsvFamily.Focus();
					this.lsvFamily.EnsureVisible(listViewItem.Index);
				}
				else
				{
					listViewItem.Selected = false;
				}
			}
		}

		private void lsvFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
			string text = this.tvFitting.SelectedNode.Text;
			if (this.lsvFamily.Items.Count > 0)
			{
				string text2 = "";
				foreach (object obj in this.lsvFamily.Items)
				{
					ListViewItem listViewItem = (ListViewItem)obj;
					if (listViewItem.Selected)
					{
						text2 = listViewItem.Text;
					}
				}
				uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(text);
				if (num <= 1988809106u)
				{
					if (num <= 39831008u)
					{
						if (num != 39565431u)
						{
							if (num != 39831008u)
							{
								return;
							}
							if (!(text == "P弯"))
							{
								return;
							}
							this.dsSetting.PBendFamilyFile = text2;
							return;
						}
						else
						{
							if (!(text == "S弯"))
							{
								return;
							}
							this.dsSetting.SBendFamilyFile = text2;
							return;
						}
					}
					else if (num != 1574458390u)
					{
						if (num != 1988809106u)
						{
							return;
						}
						if (!(text == "通气帽"))
						{
							return;
						}
						this.dsSetting.VentilationCapFamilyFile = text2;
						return;
					}
					else
					{
						if (!(text == "清扫口"))
						{
							return;
						}
						this.dsSetting.CleanMouthFamilyFile = text2;
						return;
					}
				}
				else if (num <= 2636587616u)
				{
					if (num != 2445487072u)
					{
						if (num != 2636587616u)
						{
							return;
						}
						if (!(text == "四通"))
						{
							return;
						}
						this.dsSetting.CrossFamilyFile = text2;
						return;
					}
					else
					{
						if (!(text == "地漏"))
						{
							return;
						}
						this.dsSetting.FDrainFamilyFile = text2;
					}
				}
				else if (num != 3158110106u)
				{
					if (num == 4145267678u)
					{
						if (!(text == "弯头"))
						{
							return;
						}
						this.dsSetting.ElbowFamilyFile = text2;
						return;
					}
				}
				else
				{
					if (!(text == "三通"))
					{
						return;
					}
					this.dsSetting.TeeFamilyFile = text2;
					return;
				}
			}
		}

		private void LoadFamilyToProject(PipeType pipeType)
		{
			FamilySymbol familySymbol = this.LoadFamily(this.dsSetting.ElbowFamilyFile, this.listElbow);
			this.segmentFitting.SetPipeFitting(pipeType, 1, familySymbol.Id);
			familySymbol = this.LoadFamily(this.dsSetting.TeeFamilyFile, this.listTee);
			this.segmentFitting.SetPipeFitting(pipeType, 2, familySymbol.Id);
			familySymbol = this.LoadFamily(this.dsSetting.CrossFamilyFile, this.listCross);
			this.segmentFitting.SetPipeFitting(pipeType, 3, familySymbol.Id);
			familySymbol = this.LoadFamily(this.dsSetting.SBendFamilyFile, this.listSBend);
			familySymbol = this.LoadFamily(this.dsSetting.PBendFamilyFile, this.listPBend);
			familySymbol = this.LoadFamily(this.dsSetting.CleanMouthFamilyFile, this.listCleanMouth);
			familySymbol = this.LoadFamily(this.dsSetting.VentilationCapFamilyFile, this.listVentilationCap);
			familySymbol = this.LoadFamily(this.dsSetting.FDrainFamilyFile, this.listFDrain);
		}

		private FamilySymbol LoadFamily(string fsName, List<DbFamily> listDbFamily)
		{
			FamilySymbol familySymbol = null;
			familySymbol = this.segmentFitting.GetFamilySymbolByName(fsName);
			if (familySymbol != null)
			{
				return familySymbol;
			}
			foreach (DbFamily dbFamily in listDbFamily)
			{
				if (!(dbFamily.Name != fsName))
				{
					using (List<FamilySymbol>.Enumerator enumerator2 = dbFamily.LoadInSubTrans(this.doc).GetSymbols().GetEnumerator())
					{
						if (enumerator2.MoveNext())
						{
							familySymbol = enumerator2.Current;
						}
					}
				}
			}
			return familySymbol;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnSure_Click(object sender, EventArgs e)
		{
			this.dsSetting.PipeTypeName = this.cboMaterial.SelectedItem.ToString();
			this.dsSetting.Save();
			this.CreatAndSetPipeSegment();
			this.LoadFamilyToProject(this.dsSetting.HYPipeType);
			HVACUserSetting hvacuserSetting = new HVACUserSetting();
			string[] array = new string[4];
			this.dsSetting.InitialPipeSystemType(this.doc);
			array[0] = this.dsSetting.PipeTypeName;
			array[1] = this.dsSetting.PipeTypeName;
			array[2] = this.dsSetting.PipingSystemType.Name;
			array[3] = this.dsSetting.PipingSystemType.Name;
			hvacuserSetting.writeDrainageSystemSetting(array);
			base.Close();
		}

		private bool CreatAndSetPipeSegment()
		{
			string text = this.cboMaterial.SelectedItem.ToString();
			foreach (HYMaterial hymaterial in this.dsSetting.ListMaterial)
			{
				if (hymaterial.Name == text)
				{
					this.dsSetting.Material = hymaterial;
					this.dsSetting.PipeTypeName = text;
					break;
				}
			}
			PipeType hypipeType = this.segmentFitting.CreatHYPipeTypeByName(text);
			this.dsSetting.HYPipeType = hypipeType;
			return false;
		}

		private void DrainageSystemSettingForm_Load(object sender, EventArgs e)
		{
		}

		private Document doc;

		private List<DbFamily> listElbow;

		private List<DbFamily> listTee;

		private List<DbFamily> listCross;

		private List<DbFamily> listSBend;

		private List<DbFamily> listPBend;

		private List<DbFamily> listCleanMouth;

		private List<DbFamily> listVentilationCap;

		private List<DbFamily> listFDrain;

		private List<DbFamily> listCheckMouth;

		private HYDrainageSystemSetting dsSetting;

		private OperateSegmentFitting segmentFitting;

		public HYMaterial hyMaterial;
	}
}
